Introduction

Data visualizations play an important role in helping us understand communicate data effectively. Our brains are so good a processing visual information, a good visualization can help people identify trends and relationships more easily than with plain data. This R Markdown document seeks to enhance the reader’s abilit to create powerful and memorable visualizations by demonstrating how to create animated charts using the gganimate package in R. gganimate is a powerful graphics package built on top of the ggplot2 package to create animated graphs. It provides a variety of additional functions that can be added plots that can customize how the visualizations change over time.

In addition to some general familiarity with the ggplot2 package that gganimate extends, it is important to understand how the concepts of Layers and Transitions function in the package. The gganimate animations are made up of layers with each layer containing a different type of data (e.g., data points, lines, or text). These layers can be leveraged to create vibrant illustrations to show how different datasets interact and change over time. To create the animations, gganimate provides a variety of transitions that can be employed to create simple and complex visual effects to transform static plots and graphs into dynamic and compelling visualizations.

In this demonstration, we will create two animated charts to illustrate a few of the essential functions including:

* transition_*() specifies the distribution of data and its relationship to itself across different states and time.
* view_*() specifies the scale and position of data points and how they should change over the course of the animation.
* shadow_*() specifies how prior data points should be visually represented across different states and times.
* enter_*()/exit_*() specifies how new data is introduced into the visualization adn how old data is removed from the animation.

Load Packages

# Load diamonds dataset
data(diamonds)
carat cut color clarity depth table price x y z
0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75

Static Bar Chart

First, we will create a static bar chart using the diamonds dataset from the ggplot2 package.The graph will plot the levels of clarity of the diamonds in the dataset for each type of cut (ranging from: Fair, Good, Very Good, Premium, Ideal).

# Create static bar chart
bar_chart <- ggplot(diamonds, aes(x = cut, fill = clarity)) +
  geom_bar() +
  labs(title = "Static Bar Chart", x = "Cut", y = "Count") 
bar_chart

Animated Bar Chart

To add animation to our chart, we are going to add an additional layer using the transition_states() function to our plot. The transitition_states() function will split the data into subsets based on the variable passed to it (“clarity” in this example) and calculate the intermediary data states for the animation.
After adding our transition layer, we will pass our animated object to the animate() function to render the animation in our document.

# Add transition_state animation
animated_bar_chart <- bar_chart +
  transition_states(clarity, transition_length = 2, state_length = 1)+ 
  labs(title = "Animated Bar Chart", x = "Cut", y = "Count", 
       subtitle = "Clarity: {closest_state}") #Creating a dynamic Label: the {closest_state}
                                              # object will pass the current transition that will 
                                              # pass the current transition state as the text for the subtitle

# Render animated chart
animate(animated_bar_chart)

It Works! Adding Additional Effects

Now that we have a working animation, we can look into some other functions to polish how our data transforms. The enter_*()/exit_*() functions control what happens to the data that doesn’t persist in the visualization.
There are several built-in effects such as:
enter/exit_fade()
enter/exit_grow()
enter/exit_recolor()
enter/exit_drift()

or you can specify your own transformations using: enter/exit_manual().

# Add enter_fade()/exit_fade() for a smoother effect 
animated_bar_chart_fade <- animated_bar_chart +
  enter_fade() +
  exit_fade()

# Render animated chart
animate(animated_bar_chart_fade)

Static Line Chart

To test some additional features, let’s create an animated line chart to use implement some additional functionality using the airquality dataset and creating an initial static chart as before.

# Load airquality dataset
data(airquality)
Ozone Solar.R Wind Temp Month Day
41 190 7.4 67 5 1
36 118 8.0 72 5 2
12 149 12.6 74 5 3
18 313 11.5 62 5 4
NA NA 14.3 56 5 5
# Create static line chart
line_chart <- ggplot(airquality, aes(Day, Temp, group = Month, color = factor(Month))) +
  geom_line() +
  labs(title = "Static Line Chart", subtitle = "Boring", x = "Day of Month", y = "Temperature") 
  
line_chart

Animated Line Chart

As with our bar chart, we will add the transition_reveal() function to our chart which will, in this case, render our data gradually and illustrate how our plots change over time.

# Add transition_reveal animation
animated_line_chart <- line_chart +
  labs(title = "Animated Line Chart", subtitle = "Exciting", x = "Day of Month", y = "Temperature")+
  transition_reveal(Day)

# Render animated chart
animate(animated_line_chart)

Changing the Perspective

Using the view_*() functions allows you to define how the positional scales should change along the animation and allow you to follow specific data or groups of data in a graphic. Below, I have added the view_follow() function to scale the animation in a manner that follows the data points as they are revealed along the time series.

# Adding Zoom
animated_line_chart_zoom <- animated_line_chart +
  labs(title = "Animated Line Chart", subtitle = "With Zoom", x = "Day of Month", y = "Temperature")+
  view_follow()
  
 

# Render animated chart
animate(animated_line_chart_zoom)

Adding Shadows to Data

For the final illustration, I have converted the line chart to a scatter plot, but maintained the same time-series transition along the Day variable. The plot movement on this graph will be emphasized with the shadow_wake() function that is meant to draw a small wake after a data point by showing the most recent frames up to the current point.
You can gradually diminish the size of the preceding data in the wake (as is shown here with the wake_length=0.1 proportion argument) and/or the opacity of the shadow (using the alpha= argument).

# Changing to scatter plot / adding shadow_wake effect
scatter_chart_shadow <- ggplot(airquality, aes(Day, Temp, group = Month, color = factor(Month)))+
  geom_point() +
  labs(title = "Animated Scatter Chart", x = "Day of Month", y = "Temperature")+ 
  transition_time(Day) +
  shadow_wake(wake_length = 0.5, alpha = FALSE)

# Render animated chart
animate(scatter_chart_shadow )

Summary & Resources

In this document, we have demonstrated how to create animated charts using the gganimate package in R, and highlighted some of the essential functionality of the package as we created dynamic a dynamic bar graph, line graph, and scatter plot.

You can learn more about gganimate features or the ggplot2 package it is built on in their reference manuals at cran.r-project.org:
gganimate
ggplot2
I hope this can serve as a starting point for you to explore more advanced animation techniques and customization options in the future. Happy coding!

Works Cited

This code through references and cites the following sources:

Package ‘gganimate’: A Grammar of Animated Graphics, Version 1.0.8, accessed: October 1, 2023 [Online] Available: (https://cran.r-project.org/web/packages/gganimate/gganimate.pdf)

Package ‘ggplot2’: Create Elegant Data Visualizations Using the Grammar of Graphics. Version 3.4.3, accessed: October 1, 2023 [Online] Available: (https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf)

‘gganimate’, https://gganimate.com/index.html, 1 October, 2023.